﻿#region "<---- Using Definitions ---->"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CXMLStudioIDE.DataAccessLayer;
using System.Data;
using CXMLStudioIDE.Common.Entities;

#endregion


#region "<---- Namespace Definition ---->"

namespace CXMLStudioIDE.BusinessLayer
{

    #region "<---- MainFormBL Class Region ---->"

    public class MainFormBL
    {
        public static DataSet BuildDataSet(BusinessEntity b)
        {
            string sz_Imports = "namespace Enterprise {\r\n    using System;\r\n    using System.Diagnostics;\r\n    using System.Xml.Serialization;\r\n    using System.Collections;\r\n    using System.Xml.Schema;\r\n    using System.ComponentModel;\r\n    using System.Collections.Generic;\r\n    \r\n";
            DataSet dsResults = new DataSet();
            DataSet dsWorkSet = new DataSet();
            CXMLStudioDAL CGD = new CXMLStudioDAL();
            dsWorkSet = CGD.GetBusinessEntitiesForCompilation(b.EntityType.EntityTypeID);
            System.Data.DataTable ResultTable = new DataTable("BusinessEntites");
            ResultTable.TableName = "EntityCode";
            ResultTable.Columns.Add("Code");
            dsResults.Tables.Add(ResultTable);
            System.Data.DataTable tblEntities = dsWorkSet.Tables[0];
            System.Data.DataTable tblHeaders = dsWorkSet.Tables[1];
            System.Data.DataRelation drelHeaders = new DataRelation("Headers", tblEntities.Columns[0], tblHeaders.Columns[0]);
            dsWorkSet.Relations.Add(drelHeaders);
            string sz_Code = "";
            BusinessEntityCollection innerbec = new BusinessEntityCollection();
            foreach (DataRow drEntity in tblEntities.Rows)
            {

                if (drEntity[3].ToString().Length > 1)
                {
                    sz_Code += sz_Imports;
                    foreach (DataRow drHeader in drEntity.GetChildRows(drelHeaders))
                    {
                        sz_Code += drHeader[1].ToString();
                        sz_Code += Environment.NewLine;
                    }
                    sz_Code += drEntity[3];
                    if (sz_Code.Length > 1)
                    {
                        DataRow NewRow = ResultTable.NewRow();
                        NewRow["Code"] = sz_Code + "\r\n}";
                        ResultTable.Rows.Add(NewRow);
                    }
                }
                sz_Code = "";

                BusinessEntity innerb = new BusinessEntity();
                innerb.EntityType.EntityTypeID = (int)drEntity["EntityTypeID"];
                innerb.Name = drEntity["EntityTypeName"].ToString();
                innerb.EntityVersion.FullVersion = drEntity["Version"].ToString();
                innerb.EntityVersion.EntityVersionID = (int)drEntity["EntityVersionID"];
                innerb.EntityTypeVersions.EntityTypeVersionsID = (int)drEntity["EntityTypeVersionsID"];
                innerb.EntityType.EntityTypeContextID = b.EntityType.EntityTypeContextID;
                innerb.EntityType.EntityTypeContextName = b.EntityType.EntityTypeContextName;
                innerb.EntityType.IsBusinessEntity = (bool)drEntity["IsBusinessEntity"];
                innerb.Parent = b;
                innerbec.Add(innerb);
            }
            if (innerbec !=null && innerbec.Count > 0)
            {
                b.Entities = innerbec;
            }
            return dsResults;
        }

    }

    #endregion

}

#endregion